﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using TangoGames.Base;
using TangoGames.Scene;

namespace TangoGames.Primitives3D
{
    public class ColoredPrimitive:DrawableGameComponent 
    {
        ///
        /// Shader provido pelo XNA.
        /// Reponsavel por renderizar as primitivas
        ///
        protected BasicEffect basicEffect;

        //camera
        protected ICamera camera;

        /// <summary>
        /// Matriz de "mundo" - posicionamento, escala e rotação... sempre atualizado
        /// </summary>
        protected Matrix world = Matrix.Identity;
        public Matrix World { get { return world; } set { world = value; } }

        protected Color color;

        ///
        /// Será aplicada uma textura completa
        ///
        protected VertexPositionColor[] primitives;

        protected Vector3 position;
        protected Texture2D texture;

        public ColoredPrimitive(Game game, ICamera camera, Color color)
            : base(game)
        {
            this.camera = camera;
            position = Vector3.Zero;
            this.color = color;

            this.basicEffect = new BasicEffect(game.GraphicsDevice);

            //Instanciado os 4 vertices
            this.primitives = new VertexPositionColor[6];

            primitives[0].Position = new Vector3(-0.5f ,0, -0.5f);
            primitives[0].Color = color;

            primitives[1].Position = new Vector3(0.5f, 0, -0.5f);
            primitives[0].Color = color;

            primitives[2].Position = new Vector3(0.5f, 0, 0.5f);
            primitives[0].Color = color;

            primitives[3].Position = new Vector3(-0.5f, 0, -0.5f);
            primitives[0].Color = color;

            primitives[4].Position = new Vector3(0.5f, 0, 0.5f);
            primitives[0].Color = color;

            primitives[5].Position = new Vector3(-0.5f, 0, 0.5f);
            primitives[0].Color = color;

            //Habilita a texturização; permite que a textura seja aplicada           
            this.basicEffect.TextureEnabled = false;

            //this.basicEffect.Texture = texture;

            //ativando as cores
            basicEffect.VertexColorEnabled = true;


            game.Components.Add(this);

        }

        /// <summary>
        /// Método para desenho
        /// </summary>
        /// <param name="gameTime">Tempo do jogo</param>
        public override void Draw(GameTime gameTime)
        {
            //passando o efeito básico
            basicEffect.CurrentTechnique.Passes[0].Apply();

            basicEffect.View = camera.ViewMatrix;
            basicEffect.Projection = camera.ProjectionMatrix;
            basicEffect.World = world;

            //desenhando as primitivas
            Game.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(
                PrimitiveType.TriangleList,//tipo da primitiva (linha ou triangulo)
                primitives,//o conjunto de vértices 
                0,//offset
                2);//contador de primitivas

        }

    }
}
